package com.lonely.order.support.detail.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.lonely.goods.domain.vo.goods.GoodsQuotationSheetVO;
import com.lonely.order.domain.entity.SysOrder;
import com.lonely.order.domain.vo.order.OrderDetailVO;
import com.lonely.order.domain.vo.split.GetSplitFactoryNameVO;
import com.lonely.order.service.IOrderService;
import com.lonely.system.service.ISysFactoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : 黄志标
 * @date: 2025/7/14 13:55
 * @description: 基于本地数据获取订单明细
 */
@Service
public class GetOrderDetailFromLocal extends AbstractGetOrderDetail {

    @Autowired
    private ISysFactoryService factoryService;

    @Autowired
    private IOrderService orderService;


    /**
     * 获取订单明细
     *
     * @param sysOrder
     * @return
     */
    @Override
    public OrderDetailVO getOrderDetail(SysOrder sysOrder) {
        // 组装数据
        OrderDetailVO orderDetailVO = new OrderDetailVO();
        BeanUtils.copyProperties(sysOrder, orderDetailVO);
        orderDetailVO.setFactoryName(this.factoryService.getFactoryName(sysOrder.getFactoryId()));
        orderDetailVO.setDeliveryTime(DateUtil.formatDateTime(sysOrder.getDeliveryTime()));
        orderDetailVO.setOrderPrice(sysOrder.getOrderPrice().toPlainString());
        orderDetailVO.setPlaceOrderTime(DateUtil.formatDate(sysOrder.getPlaceOrderTime()));
        orderDetailVO.setProduceOrderId(StrUtil.toString(sysOrder.getProduceOrderId()));
        orderDetailVO.setStockTypeInt(StrUtil.toString(sysOrder.getStockTypeInt()));
        orderDetailVO.setTags(super.extractOrderTags(sysOrder));

        // 设置分单工厂相关信息
        GetSplitFactoryNameVO splitFactoryNameInfo = super.getSplitFactoryNameInfo(sysOrder.getId());
        if (splitFactoryNameInfo != null) {
            orderDetailVO.setWorkshopSplitFactoryName(splitFactoryNameInfo.getWorkshopFactoryName());
            orderDetailVO.setTailSplitFactoryName(splitFactoryNameInfo.getTailFactoryName());
        }

        // 设置部件和价格
        GoodsQuotationSheetVO goodsQuotationSheet = this.orderService.getGoodsQuotationSheet(sysOrder.getId());
        orderDetailVO.setParts(goodsQuotationSheet.getParts());
        orderDetailVO.setTotalCasts(goodsQuotationSheet.getTotalCasts());
        return orderDetailVO;


    }
}
